package com.my.netty.promote.server.handler;

import com.my.netty.promote.message.GroupChatRequestMessage;
import com.my.netty.promote.message.GroupChatResponseMessage;
import com.my.netty.promote.server.session.GroupSession;
import com.my.netty.promote.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

@ChannelHandler.Sharable
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage message) throws Exception {
        String groupName = message.getGroupName();
        //暂不做存在校验
        GroupSession groupSession = GroupSessionFactory.getSession();
        //找到所有在线成员
        List<Channel> membersChannel = groupSession.getMembersChannel(groupName);
        //向每个人发送
        for (Channel channel : membersChannel) {
            channel.writeAndFlush(new GroupChatResponseMessage(message.getFrom(), message.getContent()));
        }
    }
}
